/**
 入驻小区
 **/
(function (vc) {
    var DEFAULT_PAGE = 1;
    var DEFAULT_ROWS = 10;
    vc.extends({
        data: {
            givechange: 0.0,//找零
            discount: 100.0,//折扣
            receivable: 0.0,//实际收款
            goodCashierInfo: {
                lastmoney: 0.0,
                productStocks: [],
                curCatelogId: '',
                total: 0,
                records: 1,
                moreCondition: false,
                barCode: '',
                prodName: '',
                money: 0.0,
                vipPrice: 0.0,
                bonusPoints: 0.0,
                goldCount: 0.0,
                integralCount: 0.0,
                authCode: '',
                prodNameLike: '',
                componentShow: 'goodCashierInfo',
                storeOrderUp: {
                    vipId: '',
                    totalPrice: '',
                    state: '',
                    shopId: '',
                    orderUpId: '',
                },
                vipUser: {
                    vipUserSearchValue: '',
                    vipName: '',
                    vipTel: '',
                    levelName: '',
                    sex: '',
                    integralAmount: '0',
                    vipId: '',
                    couponCount: '0'
                },
                conditions: {
                    searchValue: '',
                    page: 1,
                    row: 1,
                    shopId: vc.getCurrentCommunity().shopId
                }
            }
        },
        _initMethod: function () {
            $that._initData();
        },
        watch: {
            discount: function () {
                let validateParam = {
                    validateCode: $that.discount
                };
                $that.$emit('discount', validateParam);
            },
            receivable: function () {
                var validateParam = {
                    validateCode: $that.receivable
                };
                $that.$emit('receivable', validateParam);
            }
        },
        _initEvent: function () {
            $that.$on('discount', function (params) {
                $that.caculateMoney();
            });
            $that.$on('receivable', function (params) {
                $that.givechangeMoney();
            });
            vc.on('goodCashier', 'chooseProduct', function (_param) {
                $that._dealProduct(_param);
            });
            vc.on('goodCashier', 'clearAll', function (_param) {
                $that.clearCashier();
            });
            vc.on('goodCashier', 'storeOrderUpManage', function (_param) {
                $that.goodCashierInfo.componentShow = 'goodCashierInfo';
                if (_param) {
                    $that._queryStoreOrderCartUp(_param);
                }
            });
            vc.on('editProductNum', 'copeProductNum', function (_param) {
                $that._goodCashierEditProductStock(_param);
            });
            vc.on('pagination', 'page_event', function (_currentPage) {
                $that._listProductStocks(_currentPage, DEFAULT_ROWS);
            });
        },
        methods: {
            _initData: function () {
                document.getElementById("searchValue").focus();
            },
            _loadProduct: function () {
                var param = {
                    params: $that.goodCashierInfo.conditions
                };
                //发送get请求
                vc.http.apiGet('/productStock/searchProductStock',
                    param,
                    function (json, res) {
                        var _productStockManageInfo = JSON.parse(json);
                        let _product = _productStockManageInfo.data;
                        // 清空查询条件
                        $that.goodCashierInfo.conditions.searchValue = '';
                        if (_product == null || _product == 'undefined' || _product.length < 1) {
                            vc.toast('商品不存在');
                            return;
                        }
                        if (_product.length > 1) {
                            vc.emit('chooseProduct', 'setProducts', _product);
                            return;
                        }
                        $that._dealProduct(_product[0]);
                    }, function (errInfo, error) {
                        console.log('请求失败处理');
                    }
                );
            },
            _editProductNum: function (_stock) {
                vc.emit('editProductNum', 'openEditProductNumModel', {_stock});
            },
            //收银计算金额
            _computeCashierMoney: function () {
                let _money = 0;
                let _points = 0;
                let _cateType = 0;
                $that.goodCashierInfo.productStocks.forEach(item => {
                    _cType = item.cateType;
                    if (_cType != "3") {
                        _money += parseFloat(item.money);
                    } else {
                        _points += parseFloat(item.money);
                    }
                });
                $that.goodCashierInfo.money = _money;
                $that.goodCashierInfo.bonusPoints = _points;
                $that._computeVipUserGold();
            },
            _dealProduct: function (_product) {
                let _oldProduct = null;
                let _specName = null;
                let flag = 0;
                let vname = $that.goodCashierInfo.vipUser.vipName;
                let _cateType = _product.cateType;
                if (_cateType == "3" && vname.length < 1) {
                    vc.toast('积分商品请先登录会员！');
                    return;
                }
                _oldProduct = $that._hasExistsProduct(_product);
                if (_oldProduct != null) {
                    _oldProduct.count = parseFloat(_oldProduct.count) + 1;
                    _oldProduct.money = parseFloat(_oldProduct.prodPrice) * parseFloat(_oldProduct.count);
                    _oldProduct.money = _oldProduct.money.toFixed(2);
                    $that._computeCashierMoney();
                    return;
                }
                if (_product.productSpec) {
                    _specName = _product.productSpec.specName;
                }
                if (_product.defaultSpecValue) {
                    _specName = _product.defaultSpecValue.specName;
                }
                if (_product.vipPrice != null && _product.vipPrice != 'undefined' && _product.vipPrice != '') {
                    $that.goodCashierInfo.vipPrice = _product.vipPrice;
                }
                let price = 0.0
                if ($that.goodCashierInfo.vipUser.vipId != null && $that.goodCashierInfo.vipUser.vipId != 'undefined' && $that.goodCashierInfo.vipUser.vipId != '') {
                    price = _product.vipPrice;
                    flag = 1;
                } else {
                    price = _product.price;
                }
                $that.goodCashierInfo.productStocks.push({
                    cateType: _product.cateType,
                    productId: _product.productId,
                    valueId: _product.valueId,
                    prodName: _product.prodName,
                    prodPrice: _product.price,
                    count: 1,
                    flag: flag,
                    money: price,
                    vipPrice: _product.vipPrice,
                    vipId: $that.goodCashierInfo.vipUser.vipId,
                    coverPhoto: _product.coverPhoto,
                    specName: _specName,
                    discountType: '1001',
                    discountRate: '1',
                    discountPrice: 0,
                    storeId: _product.storeId,
                    shopId: _product.shopId
                });
                //计算总金额
                $that._computeCashierMoney();
            },
            _hasExistsProduct: function (_product) {
                let oldProduct = null;
                $that.goodCashierInfo.productStocks.forEach(item => {
                    if (_product.productId == item.productId
                        && _product.valueId == item.valueId) {
                        oldProduct = item;
                        return;
                    }
                });
                return oldProduct;
            },
            _hasExistsProductDateil: function (_product) {
                let oldProduct = null;
                $that.goodCashierInfo.productStocks.forEach(item => {
                    if (_product.productId == item.productId
                        && _product.valueId == item.valueId && _product.productSpecDetail.detailId == item.detailId) {
                        oldProduct = item;
                        return;
                    }
                });
                return oldProduct;
            },
            _openDeleteProductStockModel: function (_product) {
                let _productStocks = [];
                $that.goodCashierInfo.productStocks.forEach(item => {
                    if (_product.productId == item.productId
                        && _product.valueId == item.valueId) {
                    } else {
                        _productStocks.push(item);
                    }
                });
                $that.goodCashierInfo.productStocks = _productStocks;
                $that._computeCashierMoney();
            },
            _queryProductMethod: function () {
                $that._listProductStocks(DEFAULT_PAGE, DEFAULT_ROWS);
            },
            _computeVipUserGold: function () {
                let _vipUser = $that.goodCashierInfo.vipUser;
                if (!_vipUser.vipId) {
                    return;
                }
                if ($that.goodCashierInfo.productStocks[0].count > 1) {
                    $that.goodCashierInfo.productStocks[0].money = $that.goodCashierInfo.vipPrice * $that.goodCashierInfo.productStocks[0].count;
                    $that.goodCashierInfo.money = $that.goodCashierInfo.vipPrice * $that.goodCashierInfo.productStocks[0].count;
                }
                let _param = {
                    params: {
                        amount: $that.goodCashierInfo.money,
                        vipId: _vipUser.vipId,
                        shopId: vc.getCurrentCommunity().shopId
                    }
                }
                //发送get请求
                vc.http.apiGet('/computeGiveGold/compute',
                    _param,
                    function (json, res) {
                        let _vipUserObj = JSON.parse(json);
                        if (_vipUserObj.code != 0) {
                            vc.toast(_vipUserObj.msg);
                            return;
                        }
                        let _vipUser = _vipUserObj.data;
                        vc.copyObject(_vipUser, $that.goodCashierInfo);
                    }, function (errInfo, error) {
                        console.log('请求失败处理');
                    }
                );
            },
            _queryStoreOrderCartUp: function (storeOrderUps) {
                let _param = {
                    params: {
                        page: 1,
                        row: 50,
                        shopId: vc.getCurrentCommunity().shopId,
                        orderUpId: storeOrderUps._storeOrderUp.orderUpId
                    }
                }
                $that.goodCashierInfo.storeOrderUp.orderUpId = storeOrderUps._storeOrderUp.orderUpId;
                //发送get请求
                vc.http.apiGet('/storeOrderCartUp/queryStoreOrderCartUp',
                    _param,
                    function (json, res) {
                        var _StoreOrderCartUpyManageInfo = JSON.parse(json);
                        _StoreOrderCartUpyManageInfo.data.forEach(item => {
                            item.money = parseFloat(item.prodPrice) * parseFloat(item.count);
                            item.money = item.money - parseFloat(item.discountPrice);
                            item.money = item.money.toFixed(2);
                        })
                        $that.goodCashierInfo.productStocks = _StoreOrderCartUpyManageInfo.data;
                        $that._computeCashierMoney();
                        if (storeOrderUps._storeOrderUp.shopVipUserDto) {
                            vc.copyObject(storeOrderUps._storeOrderUp.shopVipUserDto, $that.goodCashierInfo.vipUser);
                            $that._loadVipUser();
                        }
                    }, function (errInfo, error) {
                        console.log('请求失败处理');
                    }
                );
            },
            _updateStoreOrderCartUp: function () {
                if (!$that.goodCashierInfo.storeOrderUp.orderUpId) {
                    console.log("非挂单订单");
                    return;
                }
                let _param = {
                    shopId: vc.getCurrentCommunity().shopId,
                    orderUpId: $that.goodCashierInfo.storeOrderUp.orderUpId,
                    state: '2'
                }
                vc.http.apiPost(
                    '/storeOrderUp/updateStoreOrderUp',
                    JSON.stringify(_param),
                    {
                        emulateJSON: true
                    },
                    function (json, res) {
                        //vm.menus = vm.refreshMenuActive(JSON.parse(json),0);
                        let _json = JSON.parse(json);
                        //vc.toast(_json.msg);
                    },
                    function (errInfo, error) {
                        console.log('请求失败处理');
                        vc.toast(errInfo);
                    });
            },
            _payWxCarsh: function () { // 微信收款
                vc.emit('goodCashierConfirm', 'openWxCarsh', $that.goodCashierInfo);
            },
            _openVipUser: function () {
                vc.emit('editProductNum', 'openViewVipUserModel', {});
            },
            _confirmPayCarsh() {
                vc.emit('goodCashierConfirm', 'open', $that.goodCashierInfo);
            },
            _loadVipUser: function () {
                let _vipUser = $that.goodCashierInfo.vipUser;
                if (!_vipUser.vipTel) {
                    vc.toast('请填写会员手机号');
                    return;
                }
                let _param = {
                    params: {
                        page: 1,
                        row: 1,
                        vipUserSearchValue: $that.goodCashierInfo.vipUser.vipTel,
                        shopId: vc.getCurrentCommunity().shopId
                    }
                }
                //发送get请求
                vc.http.apiGet('/shopVipUser/searchShopVipUser',
                    _param,
                    function (json, res) {
                        let _vipUserObj = JSON.parse(json);
                        if (_vipUserObj.code != 0) {
                            vc.toast(_vipUserObj.msg);
                            return;
                        }
                        let _vipUser = _vipUserObj.data;
                        vc.copyObject(_vipUser, $that.goodCashierInfo.vipUser);
                        if ($that.goodCashierInfo.vipUser.vipId != null && $that.goodCashierInfo.vipUser.vipId != 'undefined' && $that.goodCashierInfo.vipUser.vipId != '') {
                            $that.goodCashierInfo.productStocks[0].money = $that.goodCashierInfo.vipPrice;
                            $that.goodCashierInfo.productStocks[0].flag = 1;
                        }
                        $that._computeVipUserGold();
                        $that._computeCashierMoney();
                    }, function (errInfo, error) {
                        console.log('请求失败处理');
                    }
                );
            },
            clearCashier: function () {
                let _prodNameLike = $that.goodCashierInfo.prodNameLike;
                $that.receivable = 0.0;
                $that.discount = 100.0;
                $that.givechange = 0.0,
                    $that.goodCashierInfo = {
                        lastmoney: 0.0,
                        productStocks: [],
                        total: 0,
                        records: 1,
                        moreCondition: false,
                        barCode: '',
                        prodName: '',
                        money: 0.0,
                        bonusPoints: 0.0,
                        goldCount: 0.0,
                        integralCount: 0.0,
                        authCode: '',
                        prodNameLike: _prodNameLike,
                        vipUser: {
                            vipUserSearchValue: '',
                            vipName: '',
                            vipTel: '',
                            levelName: '',
                            sex: '',
                            integralAmount: '0',
                            vipId: '',
                            couponCount: '0'
                        },
                        componentShow: 'goodCashierInfo',
                        storeOrderUp: {
                            vipId: '',
                            totalPrice: '',
                            state: '',
                            shopId: ''
                        },
                        conditions: {
                            searchValue: '',
                            page: 1,
                            row: 1,
                            shopId: vc.getCurrentCommunity().shopId
                        }
                    }
            },
            _UpStoreOrder: function () {
                $that.goodCashierInfo.storeOrderUp.totalPrice = $that.goodCashierInfo.money;
                $that.goodCashierInfo.storeOrderUp.state = '0';
                $that.goodCashierInfo.storeOrderUp.vipId = '-1';
                if ($that.goodCashierInfo.vipUser.vipId) {
                    $that.goodCashierInfo.storeOrderUp.vipId = $that.goodCashierInfo.vipUser.vipId;
                }
                $that.goodCashierInfo.storeOrderUp.shopId = vc.getCurrentCommunity().shopId;
                let _param = {
                    storeOrderUp: $that.goodCashierInfo.storeOrderUp,
                    productStocks: $that.goodCashierInfo.productStocks
                }
                vc.http.apiPost(
                    '/storeOrderUp/saveStoreOrderUp',
                    JSON.stringify(_param),
                    {
                        emulateJSON: true
                    },
                    function (json, res) {
                        //vm.menus = vm.refreshMenuActive(JSON.parse(json),0);
                        let _json = JSON.parse(json);
                        if (_json.code == 0) {
                            //关闭model
                            $that.clearCashier();
                            vc.toast(_json.msg);
                            return;
                        }
                        vc.toast(_json.msg);
                    },
                    function (errInfo, error) {
                        console.log('请求失败处理');
                        vc.toast(errInfo);
                    });
            },
            _ListUpStoreOrder: function () {
                $that.goodCashierInfo.componentShow = 'storeOrderUpManage';
                vc.emit('storeOrderUpManage', 'listStoreOrderUp', {});
            },
            _getSpecValue: function (_productDetail) {
                let _specValue = '';
                _productDetail.defaultSpecValue.productSpecDetails.forEach(detail => {
                    _specValue += (detail.detailValue + "/");
                });
                if (_specValue.endsWith("/")) {
                    _specValue = _specValue.substring(0, _specValue.length - 1);
                }
                return _specValue;
            },
            _goodCashierEditProductStock: function (_param) {
                let _productStock = $that._hasExistsProduct(_param);
                vc.copyObject(_param, _productStock);
                $that._computeCashierMoney();
            }
        }
    });
})(window.vc);
